﻿window.Admin = function (options) {
    this.API = function (control, action, data, callBack, r) {
		var url = ["/api.ashx?c=", control, '&a=', action].join('');
		$.ajax({
			type: 'POST',
			url: Y.GetPath(url),
			data: data,
			dataType: "json",
			contentType: "application/json",
			beforeSend: function () {
			},
			success: function (data) {
				if (callBack != null) {
					if (data == null) {
						callBack({ "Success": false, "Message": '没有返回' });
					} else if (data.Success === false) {
					    if (data.Code === 403) {
					        if (r) {}
                            else if (window == window.top) {
					            location.href = "/login.aspx";
					        }else {
					            window.top.location.href = "/login.aspx";
					        }
						}else if(data.Code === 4001) {
						    alert(data.Message);
						}
						callBack(data);
					} else if (data.Success) {
						callBack(data.Data);
					}
				}
			}
		});
		return this;
	};

	//this.SetDatePicker = function (dStart, dEnd, format) {
	//	var sd = new Date($(dStart).val());
	//	var ed = new Date($(dEnd).val());
	//	var now = new Date();
	//	$(dStart).datetimepicker({
	//		format: format,
	//		language: 'zh-CN',
	//		showMeridian: true,
	//		autoclose: true,
	//		todayBtn: true,
	//		//'startDate': sd < now ? sd : now,
	//		'endDate':ed,
	//		//minView: 2,
	//		todayHighlight: true
	//	}).on('changeDate', function (ev) {
	//		var startTime = ev.date.valueOf();
	//		$(dEnd).datetimepicker('setStartDate', ev.date );
	//		var end = new Date($(dEnd).val()).getTime();//转毫秒
	//		if (end != "" && startTime > end) {
	//			layer.alert("开始时间不能晚于结束时间！");
	//			$(dStart).focus().val("");
	//		}
	//	});


	//	$(dEnd).datetimepicker({
	//		format: format,
	//		language: 'zh-CN',
	//		showMeridian: true,
	//		autoclose: true,
	//		todayBtn: true,
	//		'startDate': sd,
	//		//'endDate': ed>now? ed:now,
	//		//minView: 2,
	//		todayHighlight: true
	//	}).on('changeDate', function (ev) {
	//		var endTime = ev.date.valueOf();
	//		$(dStart).datetimepicker('setEndDate', ev.date);
	//		var starttime = new Date($(dStart).val()).getTime();
	//		if (starttime != "" && endTime < starttime) {
	//			layer.alert("结束时间不能早于开始时间！");
	//			$(dEnd).focus().val("");
	//		}
	//	});
	//};
	
	window.A = this;
}

    Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1, //month
            "d+": this.getDate(),    //day
            "h+": this.getHours(),   //hour
            "m+": this.getMinutes(), //minute
            "s+": this.getSeconds(), //second
            "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
            "S": this.getMilliseconds() //millisecond
        }
        if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
        return format;
    }